﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using yafc.SessionLayer;
using Interface;

namespace yafc
{
	public partial class Login : Form
	{
		#region Constants

		private readonly string TITLE_PREFIX = "登录";

		#endregion

		#region Fields

		private string serviceName;
		private Session session;

		#endregion

		#region Public Properties

		public string Username
		{
			get { return username.Text; }
			set { username.Text = value; }
		}

		public string Password
		{
			get { return password.Text; }
			set { password.Text = value; }
		}

		#endregion

		#region Constructors

		public Login(Session S)
		{
			InitializeComponent();

			session = S;
		}

		#endregion

		#region Public Methods

		public void reset()
		{
			username.Text = null;
			password.Text = null;

			username.Focus();

			labelPrompt.Visible = false;
			serviceName = null;
		}

		public void ShowDialog(string ServiceName)
		{
			serviceName = ServiceName;
			Text = TITLE_PREFIX + ServiceName;
			ShowDialog();
		}

		#endregion

		#region Non-Public Methods

		private void ok_Click(object sender, EventArgs e)
		{
			labelPrompt.Visible = true;
			if (Username.Length == 0)
			{
				labelPrompt.Text = "用户名不能为空，请重新输入。";
				username.Focus();
			}
			else if (Password.Length == 0)
			{
				labelPrompt.Text = "密码不能为空，请重新输入。";
				password.Focus();
			}
			else
			{
				labelPrompt.Text = "验证中，请稍候...";

				addAccount();
			}
		}

		private void addAccount()
		{
			IPlugin plugin;
			if (Mainframe.Plugins.TryGetValue(serviceName, out plugin))
			{
				Account acc = new Account(plugin.Factory, Username, Password);
				if (session.contains(acc))
				{
					alreadyLogged();
					return;
				}

				if (acc.login())
				{
					Hide();
					loginSucc(acc);
				}
				else
				{
					loginFailed();
				}
			}
			else
			{
				throw new Exception("Plugin for " + serviceName + " not found!");
			}
		}

		private void cancel_Click(object sender, EventArgs e)
		{
			reset();
			Hide();
		}

		private void alreadyLogged()
		{
			labelPrompt.Text = "当前会话中已登录了" + Username + "@" + serviceName;
			labelPrompt.Visible = true;
			username.Focus();
			username.SelectAll();
		}

		private void loginFailed()
		{
			if (InvokeRequired)
			{
				Invoke((MethodInvoker)(() => loginFailed()));
			}
			else
			{
				password.Focus();
				password.SelectAll();
				labelPrompt.Visible = true;
				labelPrompt.Text = "用户名或密码错误，请重新输入。";
			}
		}

		private void loginSucc(Account acc)
		{
			lock (session)
			{
				session.add(acc);
			}
		}

		private void username_Enter(object sender, EventArgs e)
		{
			if (username.Text.Length > 0)
			{
				username.SelectAll();
			}
		}

		private void password_Enter(object sender, EventArgs e)
		{
			if (password.Text.Length > 0)
			{
				password.SelectAll();
			}
		}

		#endregion
	}
}
